package com.colin.controller;

import com.colin.pojo.Users;
import com.colin.service.UsersServiceImpl;
import com.colin.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.support.RequestContext;

import javax.servlet.http.HttpSession;
import java.net.http.HttpRequest;

@Controller
@RequestMapping("/public")
public class PublicController {
    @Autowired
    private UsersServiceImpl usersServiceImpl;
    @RequestMapping("/login")
    public String login(@RequestParam(value = "redirect" , required = false , defaultValue = "") String redirect , HttpSession session){
        if (!redirect.isEmpty()){
            System.out.println("redirect=>" + redirect);
            session.setAttribute("redirect" , redirect);
        }
        return "public/login";
    }

    @PostMapping("/login")
    public String doLogin(String user , String pass , HttpSession session , Model model){
        if (user.equals("")){
            model.addAttribute("error" , "用户名不能为空");
            return "message";
        }
        if (pass.equals("")){
            model.addAttribute("error" , "密码不能为空");
            return "message";
        }
        Users users = usersServiceImpl.queryByUser(user);
        if (users == null){
            model.addAttribute("error" , "用户信息不存在");
            return "message";
        }
        if (!users.getPass().equals(pass)){
            model.addAttribute("error" , "密码不正确");
            return "message";
        }
        session.setAttribute("userInfoId" , users.getId());
        Object redirect = session.getAttribute("redirect");
        if (redirect != null){
            session.removeAttribute("redirect");
            return "redirect:" + redirect;
        }
        return "redirect:/";
    }

    @RequestMapping("/register")
    public String register(){
        return "public/register";
    }

    @PostMapping("/register")
    public String doRegister(String user , String pass , String repass , Model model , HttpSession session){
        if (user.equals("") || pass.equals("") || repass.equals("")){
            model.addAttribute("error" , "用户名或密码未填写");
            return "message";
        }
        if (!pass.equals(repass)){
            model.addAttribute("error" , "两次密码输入不一致");
            return "message";
        }
        Users userFind = usersServiceImpl.queryByUser(user);
        if (userFind != null){
            model.addAttribute("error" , "用户名已经被使用了");
            return "message";
        }
        Users users = new Users();
        users.setUser(user);
        users.setPass(pass);
        users.setCreateTime(TimeUtils.getTime());
        int res = usersServiceImpl.addUser(users);
        if (res > 0){
            session.setAttribute("userInfoId" , users.getId());
            return "redirect:/";
        }else{
            model.addAttribute("error" , "注册失败");
            return "message";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("userInfoId");
        return "redirect:/public/login";
    }
}
